<template>
	<view class="">
		nfc
		<!-- 弹窗提示 -->
		<model :title="title" :maskClick="false" @cancle="visibleShow = false" @comfire="visibleShow = false" :visible="visibleShow">
			<view class="modelCont">
				<view>设备内容:{{objs.payload||"--"}}</view>
				<view>设备id:{{objs.id||"--"}}</view>
				<view>设备类型:{{objs.type||"--"}}</view>
			</view>
		</model>
	</view>
</template>

<script>
import model from '@/components/model/index.vue';
export default {
	data() {
		return {
			title:'',
			visibleShow:false,
			objs:{
					
			}
		};
	},
	components:{
		model
	},
	onLoad() {
		const NFCAdapter = wx.getNFCAdapter();
		console.log('NFCAdapter',NFCAdapter);
				
		NFCAdapter.startDiscovery({
			success: res => {
				this.title = '请将设备放入识别区NFC';
				console.log('请将设备放入识别区NFC',res);
			},
			fail: error => {
				this.title = '刷新重试';
				console.error('刷新重试',error);
			},
			complete: res => {
				this.visibleShow = true;
				console.log(res);
			}
		});
		NFCAdapter.onDiscovered(callback => {
			console.log('onDiscovered callback=>', callback);
			let aid = parseInt(this.ab2hex(callback.id), 16);
			console.log('onDiscovered 对象aid', aid);
			if (callback.messages) {
				this.visibleShow = true;
				let cordsArray = callback.messages[0].records;
				let obj = {};
				cordsArray.find(item => {
					obj.payload = this.byteToString(new Uint8Array(item.payload),true);
					obj.id = this.byteToString(new Uint8Array(item.id));
					obj.type = this.byteToString(new Uint8Array(item.type));
				});
				console.log('onDiscovered 对象=>', obj);
				this.title = '数据';
				this.objs.payload = obj.payload;
				this.objs.id = obj.id;
				this.objs.type = obj.type;
			}
			if (callback.techs.length != 0) {
				// this.title = '识别成功！';
				// this.content = '可支持标签：';
				console.log('识别成功 可支持标签',callback.techs );
				callback.techs.forEach((res, index) => {
					console.log(res);
					if (index != 0) {
						this.content += '、';
					}
					this.content += res;
					// 支持写入
					if (res == 'NDEF') {
						this.isWrite = true;
					}
				});
			} else {
				this.title = '无效设备';
				console.log('无效设备');
			}
		});
	},
	methods:{
		/* 设备标签 */
		initTab(item) {
			let NFCTab = tab[item];
			NFCTab.connect({
				success: res => {
					// this.title = '连接设备成功';
					console.log('连接设备成功',res);
				},
				fail: error => {
					// this.title = '连接设备失败';
					console.error('连接设备失败',error);
				},
				complete: res => {
					console.log(res);
				}
			});
			return NFCTab;
		},
		ab2hex(buffer) {
			var hexArr = Array.prototype.map.call(
				new Uint8Array(buffer),
			
				function(bit) {
					return ('00' + bit.toString(16)).slice(-2);
				}
			);
			return hexArr.join('');
		},
		// ArrayBuffer转字符串
		ab2str(buf) {
			return String.fromCharCode.apply(null, new Uint8Array(buf));
		},
		byteToString(arr,types) {
			if (typeof arr === 'string') {
				return arr;
			}
			console.log('arrarr',arr);
			var str = '',
				_arr = arr;
				
			for (var i = 0; i < _arr.length; i++) {
				var one = _arr[i].toString(2),
					v = one.match(/^1+?(?=0)/);
				if (v && one.length == 8) {
					var bytesLength = v[0].length;
					var store = _arr[i].toString(2).slice(7 - bytesLength);
					for (var st = 1; st < bytesLength; st++) {
						store += _arr[st + i].toString(2).slice(2);
					}
					str += String.fromCharCode(parseInt(store, 2));
					i += bytesLength - 1;
				} else {
					str += String.fromCharCode(_arr[i]);
				}
			}
			if (types) {
				// let strType = str.substring(0,2)
				// console.log("接触",str.replace(/[en|cn]/g,""))
				return str.replace(/[en|cn]/g,'') || '--';
			}
			return str;
		},
	}
};
</script>

<style>
</style>
